<?php


namespace Backend\Modules\Video\Controllers;
use Common\library\Common;
use Library\Constant\Constant;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Models\VideoCategory;

class AjaxVideoCategoryController extends \BackendController
{
	public function loadVideoCategoryAction()
	{
		$this->view->setTemplateAfter('empty');

        $videoCategories = VideoCategory::fetchCategoryTree(0 , $spacing = '' , $user_tree_array = '');
        $data = array();
        foreach($videoCategories as $category => $key)
        {
            $videoCategory = VideoCategory::findFirstById($category);
            $videoCategory->setName($key);
            $data[] = $videoCategory;
        }

        $this->view->objectType = $this->request->get('object_type');
        $this->view->objectId = $this->request->get('object_id');
        $this->view->videoCategories = $data;
        $this->view->formSearchVideo = $formSearchVideo;
        $this->view->render("ajax-video-category", "modal/modal_category");
	}

	public function addVideoCategoryToObjectAction()
	{
		if(!$this->request->has('object_type'))
        {
            return ;
        }
        $objectType  = $this->request->get('object_type');
        switch($objectType)
        {
            case 'video':
                if($this->request->hasPost('selectVideo'))
                {
                    $selectVideos = $this->request->getPost('selectVideo');
                    $data = array();
                    foreach($selectVideos as $videoId)
                    {
                        $checkVideoHaveVideoCategoryLink = \Backend\Modules\Video\Models\VideoCategoryLink::find(array(
                            "object_id = $videoId AND category_id = 1"
                        ))->count();
                        if($checkVideoHaveVideoCategoryLink <= 0)
                        {
                            $selectVideo = Video::findFirstById($videoId);
                            if($selectVideo)
                            {
                                $videoPosterLink = new \Backend\Modules\Video\Models\VideoCategoryLink();
                                
                                if(!$videoPosterLink->save())
                                {
                                    $this->flash->error($videoPosterLink->getMessages());
                                    die(print '123');
                                }
                            }
                        }
                    }
                }
                break;

            case 'video-group':
                if($this->request->hasPost('selectVideo'))
                {
                    $objectId = $this->request->get('object_id');

                    $selectVideos = $this->request->getPost('selectVideo');
                    $data = array();
                    foreach($selectVideos as $videoId)
                    {
                        $checkVideoHaveVideoGroup = \Backend\Modules\Video\Models\VideoGroupLink::find(array(
                            "video_id = $videoId AND video_group_id = $objectId"
                        ))->count();

                        if($checkVideoHaveVideoGroup <= 0)
                        {
                            $selectVideo = Video::findFirstById($videoId);
                            if($selectVideo)
                            {
                                $videoGroupLink = new \Backend\Modules\Video\Models\VideoGroupLink();
                                $videoGroupLink->setVideoId($videoId);
                                $videoGroupLink->setVideoGroupId($objectId);
                                $videoGroupLink->setPosition(0);
                                $videoGroupLink->save();
                            }
                        }
                    }
                }
                break;
        }
        return $this->response->redirect($this->request->getHTTPReferer());
	}
}